package com.example.crm01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm01.entity.Client;
import com.example.crm01.mapper.ClientMapper;
import com.example.crm01.service.ClientService;
import com.example.crm01.service.form.client.ClientAddForm;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.client.ClientListForm;
import com.example.crm01.service.vo.client.ClientMonthSumVO;
import com.example.crm01.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;


@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements ClientService {


    private final ClientMapper clientMapper;

    public ClientServiceImpl(ClientMapper clientMapper) {
        this.clientMapper = clientMapper;
    }

    @Override
    public Page<Client> selectClientList(ClientListForm form) {
        Page<Client> page = new Page<>(form.getPageNum(), form.getPageSize());

        LambdaQueryWrapper<Client> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(form.getClientName())) {
            queryWrapper.eq(Client::getClientName, form.getClientName());
        }
        if (ObjectUtils.isNotEmpty(form.getPhone())) {
            queryWrapper.eq(Client::getPhoneOne, form.getPhone());
        }
        queryWrapper.ne(Client::getDel_flag, 2);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean insertClient(ClientAddForm form) {
        Client client = new Client();
        client.setClientName(form.getClientName());
        client.setClientFormerName(form.getClientFormerName());
        client.setIdCard(form.getIdCard());
        client.setAddress(form.getAddress());
        client.setRegion(form.getRegion());
        client.setHomeLocation(form.getHomeLocation());
        client.setContacts(form.getContacts());
        client.setPhoneOne(form.getPhoneOne());
        client.setPhoneTwo(form.getPhoneTwo());
        client.setClientManger(form.getClientManger());
        client.setMangerPhone(form.getMangerPhone());
        client.setSalesman(form.getSalesman());

        //-----------
        client.setCreateBy("1");
        client.setCreateTime(LocalDateTime.now());
        client.setUpdateBy("1");
        client.setUpdateTime(LocalDateTime.now());
        client.setDel_flag(0);

        int insert = clientMapper.insert(client);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateClient(ClientAddForm form) {
        if (ObjectUtils.isEmpty(form.getId())) {
            throw new RuntimeException("修改客户信息，主键id不能为空");

        }
        Client client = baseMapper.selectById(form.getId());
        client.setClientName(form.getClientName());
        client.setClientFormerName(form.getClientFormerName());
        client.setIdCard(form.getIdCard());
        client.setAddress(form.getAddress());
        client.setRegion(form.getRegion());
        client.setHomeLocation(form.getHomeLocation());
        client.setContacts(form.getContacts());
        client.setPhoneOne(form.getPhoneOne());
        client.setPhoneTwo(form.getPhoneTwo());
        client.setClientManger(form.getClientManger());
        client.setMangerPhone(form.getMangerPhone());
        client.setSalesman(form.getSalesman());

        //-----------
        client.setUpdateBy("1");
        client.setUpdateTime(LocalDateTime.now());
        client.setDel_flag(2);
        int update = baseMapper.updateById(client);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteClient(BaseIdForm form) {
        Client client = baseMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(client)) {
            throw new RuntimeException("客户信息不存在删除失败");
        }
        client.setUpdateBy("1");
        client.setUpdateTime(LocalDateTime.now());
        client.setDel_flag(2);
        int update = baseMapper.updateById(client);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Client detailsClient(BaseIdForm form) {
        Client client = baseMapper.selectById(form.getId());
        if (ObjectUtils.isEmpty(client)) {
            throw new RuntimeException("客户信息不存在,获取详情信息失败");
        }
        return client;
    }

    @Override
    public ClientMonthSumVO searchClientMonth() {
        LambdaQueryWrapper<Client> allClient = new LambdaQueryWrapper<>();
        allClient.ne(Client::getDel_flag, 2);
        Long count = baseMapper.selectCount(allClient);

        LocalDateTime now = LocalDateTime.now();
        //当月的第一天的00:00:00
        LocalDateTime firstDay = LocalDateTime.of(now.with(TemporalAdjusters.firstDayOfMonth()).toLocalDate(), LocalTime.MIN);
        //当月的第一天的23:59:59
        LocalDateTime lastDay = LocalDateTime.of(now.with(TemporalAdjusters.lastDayOfMonth()).toLocalDate(),LocalTime.MAX);

        LambdaQueryWrapper<Client> delClient = new LambdaQueryWrapper<>();
        delClient.between(Client::getUpdateTime,firstDay,lastDay);
        delClient.eq(Client::getDel_flag, 2);
        Long nowMonthDelClientNum = baseMapper.selectCount(delClient);

        LambdaQueryWrapper<Client> addClient = new LambdaQueryWrapper<>();
        addClient.between(Client::getCreateTime,firstDay,lastDay);
        addClient.ne(Client::getDel_flag, 2);
        Long nowMonthAddClientNum = baseMapper.selectCount(addClient);

        ClientMonthSumVO vo = new ClientMonthSumVO();
        vo.setAddClientNum(count);
        vo.setNowMonthDelClientNum(nowMonthDelClientNum);
        vo.setNowMonthAddClientNum(nowMonthAddClientNum);
        return vo;
    }
}
